<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/10 0010
 * Time: 上午 9:48
 */
namespace app\admin\service;

use app\admin\model\SupervisionUsers;
use app\admin\model\User;
use app\common\redis\Redis;

class Supervision{


    public function getStatistics($param = []){
        if(empty($param) || (empty($param['departmentId']) && empty($param['personId']))){
            //全部
            $data = \app\admin\model\Supervision::where(true)->select();
            $work_internal = \app\admin\model\Supervision::where('typeId',1)->count();
            //内部工作评价
            $join = [
                ['supervision s','s.id=a.supervisionId']
            ];
            $field = 'a.estimate,a.supervisionId,s.id,s.typeId';
            $res = db('supervision_estimate')->alias('a')->where('s.typeId',1)->join($join)->field($field)->select();
            //信访评价
            $join = [
                ['letter l','l.id=a.letterId']
            ];
            $field = 'a.typeId,a.letterId,l.reply';
            $lettermodel = db('supervision')->alias('a')->where('a.typeId',2)->where('l.reply','not null')->join($join)->field($field)->select();

        }else{
            $departmentId = $param['departmentId'];
            $personId = $param['personId'];
//            $s_timer = $param['s_complete_time'];
//            $e_timer = $param['e_complete_time'];
            if(!empty($departmentId) && empty($personId)){
                //部门统计

                $data = \app\admin\model\SupervisionUsers::where('dept_id',$departmentId)->group('supervisionId')->select();
                if($data){
                    foreach ($data as &$v){
                        $Supervision = \app\admin\model\Supervision::get($v['supervisionId']);
                        $v['complete_time'] = $Supervision['complete_time'];
                        $v['actual_time'] = $Supervision['actual_time'];
                        $v['schedule'] = $Supervision['schedule'];
                    }
                }
                $join = [
                    ['supervision s','s.id=a.supervisionId']
                ];
                $field = 'a.supervisionId,a.dept_id,s.id,s.typeId';
                $work_internal = db('supervision_users')->alias('a')->join($join)->where(['a.dept_id'=>$departmentId,'s.typeId'=>1])->group('a.supervisionId')->field($field)->count();
                //内部工作评价
                $res = db('supervision_estimate')->field('supervisionId,estimate')->select();
                $listarr = [];
                foreach ($res as $val){
                    $Supervision = \app\admin\model\Supervision::where(['id'=>$val['supervisionId'],'typeId'=>1])->find();
                    if($Supervision){
                        array_push($listarr,['id'=>$Supervision->id,'estimate'=>$val['estimate']]);
                    }

                }
                $listn = [];
                if($listarr){
                    foreach ($listarr as &$v){
                        $SupervisionUsers = SupervisionUsers::where('supervisionId',$v['id'])->group('dept_id')->field('dept_id')->select();
                        foreach ($SupervisionUsers as $value){
                            if($value['dept_id'] == $departmentId){
                                array_push($listn,['dept_id'=>$value['dept_id'],'estimate'=>$v['estimate']]);
                            }

                        }
                    }
                }

                //信访评价
                $lettermodel = db('letter')->where('reply','not null')->field('id,reply')->select();
                if($lettermodel){
                    $listarr = [];
                    foreach ($lettermodel as $v){
                        $Supervision = \app\admin\model\Supervision::where('letterId',$v['id'])->find();
                        array_push($listarr,['id'=>$Supervision->id,'reply'=>$v['reply']]);
                    }

                    $listx = [];
                    if($listarr){
                        foreach ($listarr as &$v){
                            $SupervisionUsers = SupervisionUsers::where('supervisionId',$v['id'])->group('dept_id')->field('dept_id')->select();
                            foreach ($SupervisionUsers as $value){
                                if($value['dept_id'] == $departmentId){
                                    array_push($listx,['dept_id'=>$value['dept_id'],'reply'=>$v['reply']]);
                                }

                            }
                        }
                    }

                }
            }
            if(!empty($departmentId) && !empty($personId)){
                //个人
                $work_internal = 0;
                $data = \app\admin\model\SupervisionUsers::where('user_id',$personId)->field('supervisionId')->select();
                if($data){
                    foreach ($data as &$v){
                        $Supervision = \app\admin\model\Supervision::get($v['supervisionId']);
                        $v['complete_time'] = $Supervision['complete_time'];
                        $v['actual_time'] = $Supervision['actual_time'];
                        $v['schedule'] = $Supervision['schedule'];
                    }
                }
                if($data){
                    $listarr = [];
                    foreach ($data as $v){
                        $Supervision = \app\admin\model\Supervision::where(['id'=>$v['supervisionId'],'typeId'=>1])->field('id,typeId')->find();
                        if($Supervision){
                            array_push($listarr,$Supervision->id);
                        }

                    }
                    $work_internal = count($listarr);
                }
                //内部工作评价
                $res = db('supervision_estimate')->field('supervisionId,estimate')->select();
                $listarr = [];
                foreach ($res as $val){
                    $Supervision = \app\admin\model\Supervision::where(['id'=>$val['supervisionId'],'typeId'=>1])->find();
                    if($Supervision){
                        array_push($listarr,['id'=>$Supervision->id,'estimate'=>$val['estimate']]);
                    }

                }
                $listn = [];
                if($listarr){
                    foreach ($listarr as &$v){
                        $SupervisionUsers = SupervisionUsers::where('supervisionId',$v['id'])->where('user_id',$personId)->field('user_id')->select();
                        foreach ($SupervisionUsers as $value){
                            array_push($listn,['user_id'=>$value['user_id'],'estimate'=>$v['estimate']]);
                        }
                    }
                }



                //信访评价
                $lettermodel = db('letter')->where('reply','not null')->field('id,reply')->select();
                if($lettermodel){
                    $listarr = [];
                    foreach ($lettermodel as $v){
                        $Supervision = \app\admin\model\Supervision::where('letterId',$v['id'])->find();
                        array_push($listarr,['id'=>$Supervision->id,'reply'=>$v['reply']]);
                    }

                    $listx = [];
                    if($listarr){
                        foreach ($listarr as &$v){
                            $SupervisionUsers = SupervisionUsers::where('supervisionId',$v['id'])->where('user_id',$personId)->field('user_id')->select();
                            foreach ($SupervisionUsers as $value){
                                array_push($listx,['user_id'=>$value['user_id'],'reply'=>$v['reply']]);
                            }
                        }
                    }

                }
            }
        }

        $work_visit = count($data) - $work_internal;
        $complete_all = 0;
        $complete_all_overdue = 0;
        $hangintheairnum = 0;
        if($data){
            foreach ($data as $v){
                $intcomplete_time = (int)strtotime($v['complete_time']);
                if($v['schedule'] == 3){
                    $intactual_time = (int)strtotime($v['actual_time']);
                    if(($intcomplete_time+3600*24) > $intactual_time){
                        $complete_all++;
                    }else{
                        $complete_all_overdue++;
                    }
                }else{
                    if(($intcomplete_time+3600*24) < time()){
                        $hangintheairnum++;
                    }
                }

            }
        }
        $internal_satisfaction = 0;
        $internal_basic = 0;
        $internal_no = 0;
        $visit_satisfaction = 0;
        $visit_basic = 0;
        $visit_no = 0;
        if(empty($param) || (empty($param['departmentId']) && empty($param['personId']))){
            if($res){
                foreach ($res as $value){
                    //内部工作评价
                    if($value['estimate'] == 1){
                        $internal_satisfaction++;
                    }elseif ($value['estimate'] == 2){
                        $internal_basic++;
                    }else{
                        $internal_no++;
                    }
                }
            }
            if($lettermodel){
                foreach ($lettermodel as $v){
                    if($v['reply'] == 1){
                        $visit_satisfaction++;
                    }elseif ($v['reply'] == 2){
                        $visit_basic++;
                    }else{
                        $visit_no++;
                    }
                }
            }
        }else{
            if(!empty($departmentId) && empty($personId)){
                if(isset($listn) && !empty($listn)){
                    foreach ($listn as $v){
                        if($v['dept_id'] == $departmentId){
                            if($v['estimate'] == 1){
                                $internal_satisfaction++;
                            }elseif ($v['estimate'] == 2){
                                $internal_basic++;
                            }else{
                                $internal_no++;
                            }
                        }
                    }
                }
                if(isset($listx) && !empty($listx)){
                    foreach ($listx as $v){
                        if($v['dept_id'] == $departmentId){
                            if($v['reply'] == 1){
                                $visit_satisfaction++;
                            }elseif ($v['reply'] == 2){
                                $visit_basic++;
                            }else{
                                $visit_no++;
                            }
                        }
                    }
                }
            }
            if(!empty($departmentId) && empty($personId)){
                if(isset($listn) && !empty($listn)){
                    foreach ($listn as $v){
                        if($v['estimate'] == 1){
                            $internal_satisfaction++;
                        }elseif ($v['estimate'] == 2){
                            $internal_basic++;
                        }else{
                            $internal_no++;
                        }
                    }
                }
                if(isset($listx) && !empty($listx)){
                    foreach ($listx as $v){
                        if($v['reply'] == 1){
                            $visit_satisfaction++;
                        }elseif ($v['reply'] == 2){
                            $visit_basic++;
                        }else{
                            $visit_no++;
                        }
                    }
                }
            }
        }
        $result['work_all'] = count($data);
        $result['work_internal'] = $work_internal;
        $result['work_visit'] = $work_visit;
        $result['complete_all'] = $complete_all;
        $result['hangintheairnum'] = $hangintheairnum;
        $result['complete_all_overdue'] = $complete_all_overdue;
        $result['internal_satisfaction'] = $internal_satisfaction;
        $result['internal_basic'] = $internal_basic;
        $result['internal_no'] = $internal_no;
        $result['visit_satisfaction'] = $visit_satisfaction;
        $result['visit_basic'] = $visit_basic;
        $result['visit_no'] = $visit_no;
        return $result;
    }

    public function senMsg($data,$userIds){
        if(!is_array($userIds)){
            $res = [];
            $res[0] = $userIds;
            $userIds = $res;
        }
        $user = User::where('id','in',array_unique($userIds))->select();
        if($user){
            $redis = Redis::getRedisConn();
            if($data){
                foreach ($user as $v){
                    $data['p_phone'] = $v['p_phone'];
                    $redis->RPush('supervision_mobile_list',json_encode($data));
                }
            }
        }
    }
}